﻿using System;
using System.Collections.Generic;
//
using PerceptiveMCAPI;
//

namespace PerceptiveMCAPI_Test
{
    public partial class get_campaigns : System.Web.UI.UserControl
    {
        public ApiMethod method { get; set; }

        public Dictionary<string, object> filters
        {
            get { return GetFilters(); }
        }

        public int start
        {
            get
            {
                int it; int.TryParse( tbStart.Text.Trim(), out it );
                return it;
            }
            private set { tbStart.Text = value.ToString(); }
        }
        public int limit
        {
            get
            {
                int it; int.TryParse( tbLimit.Text.Trim(), out it );
                return it;
            }
            private set { tbLimit.Text = value.ToString(); }
        }


        // ------------------------------------------------------------
        public void Setup ()
        {
            this.Visible = true;
            lblSection.Text = method.section;
            lblMethod.Text = method.name;
            cbExact.Focus();
            start = MCAPISettings.default_start;
            limit = MCAPISettings.default_limit_campaigns;
            ddlCampaignStatus.DataSource = EnumValues.campaignStatusValues();
            ddlCampaignStatus.DataBind();
            ddlCampaignStatus.SelectedIndex = -1;
            ddlCampaignType.DataSource = EnumValues.campaignTypeValues();
            ddlCampaignType.DataBind();
            ddlCampaignType.SelectedIndex = -1;
        }

        private Dictionary<string, object> GetFilters ()
        {
            var filters = new Dictionary<string, object>();
            DateTime dt; int it;
            //
            if ( !string.IsNullOrEmpty( tbCampaign_id.Text ) ) filters.Add( "campaign_id", tbCampaign_id.Text );
            if ( !string.IsNullOrEmpty( tbList_id.Text ) ) filters.Add( "list_id", tbList_id.Text );
            if ( !string.IsNullOrEmpty( tbFolder_id.Text ) )
            {
                int.TryParse( tbFolder_id.Text, out it );
                filters.Add( "folder_id", it );
            }
            if ( !string.IsNullOrEmpty( tbFrom_name.Text ) ) filters.Add( "from_name", tbFrom_name.Text );
            if ( !string.IsNullOrEmpty( tbFrom_email.Text ) ) filters.Add( "from_email", tbFrom_email.Text );
            if ( !string.IsNullOrEmpty( tbTitle.Text ) ) filters.Add( "title", tbTitle.Text );
            if ( !string.IsNullOrEmpty( tbSubject.Text ) ) filters.Add( "subject", tbSubject.Text );
            if ( !string.IsNullOrEmpty( tbTime_start.Text ) )
            {
                DateTime.TryParse( tbTime_start.Text, out dt );
                filters.Add( "sendtime_start", dt );
            }
            if ( !string.IsNullOrEmpty( tbTime_end.Text ) )
            {
                DateTime.TryParse( tbTime_end.Text, out dt );
                filters.Add( "sendtime_end", dt );
            }
            if ( ddlCampaignStatus.SelectedIndex > 0 )
                filters.Add( "status", ApiHelper.GetEnumFromString<EnumValues.campaign_status>( ddlCampaignStatus.SelectedItem.Text ) );
            if ( ddlCampaignType.SelectedIndex > 0 )
                filters.Add( "type", ApiHelper.GetEnumFromString<EnumValues.campaign_type>( ddlCampaignType.SelectedItem.Text ) );
            //
            filters.Add( "exact", cbExact.Checked );
            //
            return filters;
        }
    }
}